package com.controller.merchant;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import com.dto.merchant.GuestDTO;
import com.dto.merchant.OrderDTO;
import com.dto.merchant.OrderItemsDTO;
import com.dto.merchant.UserDTO;
import com.model.dao.OrderDAO;
import com.model.dao.OrderItemsTable;
import com.model.dao.OrderTable;

@ManagedBean(name = "orderController")
@SessionScoped
public class OrderController implements Serializable {

	private static final long serialVersionUID = 7187300770392997429L;

	public List<OrderDTO> findAll() {

		OrderDAO dao = new OrderDAO();
		List<OrderTable> list = dao.findAll();
		List<OrderDTO> orderList = new ArrayList<OrderDTO>();
		for (OrderTable table : list) {
			OrderDTO dto = new OrderDTO();
			dto.setOid(table.getOrderId() + "");
			dto.setOrderDate(table.getDateOfOrder());
			dto.setStatus(table.getOrderStatus());
			dto.setOrderType(table.getOrderType());
			List<OrderItemsDTO> itemsTables = new ArrayList<OrderItemsDTO>();
			for (OrderItemsTable itemsTable : table.getOrderItemsTables()) {
				OrderItemsDTO itemsDTO = new OrderItemsDTO();
				itemsDTO.setOrderItemId(itemsTable.getIdOrderItemsTable());
				itemsDTO.setOrderItemName(itemsTable.getChildcatalogTable()
						.getMastercatalogTable().getItemBrand()
						+ itemsTable.getChildcatalogTable()
								.getMastercatalogTable().getItemName()
						+ itemsTable.getChildcatalogTable().getChildItemSize());
				itemsDTO.setOrderItemQty(itemsTable.getItemQty());
				itemsDTO.setOrderItemImage(itemsTable.getChildcatalogTable()
						.getChildItemImagePath());
				itemsTables.add(itemsDTO);
			}
			dto.setItemsList(itemsTables);
			if (!table.getOrderType().equalsIgnoreCase("G")
					&& null != table.getUserTable()) {
				UserDTO userDTO = new UserDTO();
				userDTO.setArea(table.getUserTable().getArea());
				userDTO.setCity(table.getUserTable().getCity());
				userDTO.setEmail(table.getUserTable().getEmail());
				userDTO.setHouseno(table.getUserTable().getHouseno());
				userDTO.setLandmark(table.getUserTable().getLandmark());
				userDTO.setMobileno(table.getUserTable().getMobileno());
				userDTO.setName(table.getUserTable().getName());
				userDTO.setStreet(table.getUserTable().getStreet());
				dto.setUserDTO(userDTO);
			}
			if (table.getOrderType().equalsIgnoreCase("G")
					&& null != table.getGuestTable()) {
				GuestDTO guestDTO = new GuestDTO();
				guestDTO.setArea(table.getGuestTable().getArea());
				guestDTO.setCity(table.getGuestTable().getCity());
				guestDTO.setEmail(table.getGuestTable().getEmail());
				guestDTO.setHouseno(table.getGuestTable().getHouseno());
				guestDTO.setLandmark(table.getGuestTable().getLandmark());
				guestDTO.setMobileno(table.getGuestTable().getMobileno());
				guestDTO.setName(table.getGuestTable().getName());
				guestDTO.setStreet(table.getGuestTable().getStreet());
				dto.setGuestDTO(guestDTO);
			}
			orderList.add(dto);
		}
		return orderList;
	}

	public static void main(String[] args) {
		OrderController controller = new OrderController();
		controller.findAll();
	}

	private List<OrderDTO> orderList;

	public List<OrderDTO> findMyOrders(int id) {
		OrderDAO dao = new OrderDAO();
		List<OrderTable> list = dao.findMyOrders(id);
		orderList = new ArrayList<OrderDTO>();
		for (OrderTable table : list) {
			OrderDTO dto = new OrderDTO();
			dto.setOid(table.getOrderId() + "");
			dto.setOrderDate(table.getDateOfOrder());
			dto.setStatus(table.getOrderStatus());
			dto.setDeliveryDate(table.getDateOfDelivery());
			dto.setAmount(table.getPrice());
			List<OrderItemsDTO> itemsTables = new ArrayList<OrderItemsDTO>();
			for (OrderItemsTable itemsTable : table.getOrderItemsTables()) {
				OrderItemsDTO itemsDTO = new OrderItemsDTO();
				itemsDTO.setOrderItemId(itemsTable.getIdOrderItemsTable());
				itemsDTO.setBrand(itemsTable.getChildcatalogTable()
						.getMastercatalogTable().getItemBrand());
				itemsDTO.setOrderItemName(itemsTable.getChildcatalogTable()
						.getMastercatalogTable().getItemName());
				itemsDTO.setSize(itemsTable.getChildcatalogTable()
						.getChildItemSize());
				itemsDTO.setOrderItemQty(itemsTable.getItemQty());
				itemsDTO.setOrderItemImage(itemsTable.getChildcatalogTable()
						.getChildItemImagePath());
				itemsDTO.setPrice(itemsTable.getChildcatalogTable()
						.getChildItemSrp().doubleValue());
				itemsTables.add(itemsDTO);
			}
			dto.setItemsList(itemsTables);
			orderList.add(dto);
		}
		return orderList;
	}

	public void updateOrder(OrderDTO dto) {
		OrderDAO dao = new OrderDAO();
		dao.updateStatus(Integer.valueOf(dto.getOid()), dto.getStatus());
	}

	public List<OrderDTO> findOrdersDaily() {
		OrderDAO dao = new OrderDAO();
		List<OrderTable> list = dao.findOrdersFromDate(new Date(), new Date());
		List<OrderDTO> orderList = new ArrayList<OrderDTO>();
		for (OrderTable table : list) {
			OrderDTO dto = new OrderDTO();
			dto.setOid(table.getOrderId() + "");
			dto.setOrderDate(table.getDateOfOrder());
			dto.setStatus(table.getOrderStatus());
			dto.setDeliveryDate(table.getDateOfDelivery());
			dto.setAmount(table.getPrice());
			orderList.add(dto);
		}
		return orderList;
	}

	public List<OrderDTO> findOrdersWeekly() {
		OrderDAO dao = new OrderDAO();
		List<OrderTable> list = dao.findOrdersFromDate(new Date(), new Date());
		List<OrderDTO> orderList = new ArrayList<OrderDTO>();
		for (OrderTable table : list) {
			OrderDTO dto = new OrderDTO();
			dto.setOid(table.getOrderId() + "");
			dto.setOrderDate(table.getDateOfOrder());
			dto.setStatus(table.getOrderStatus());
			dto.setDeliveryDate(table.getDateOfDelivery());
			dto.setAmount(table.getPrice());
			orderList.add(dto);
		}
		return orderList;
	}

	public List<OrderDTO> findOrdersMonthly() {
		OrderDAO dao = new OrderDAO();
		List<OrderTable> list = dao.findOrdersFromDate(new Date(), new Date());
		List<OrderDTO> orderList = new ArrayList<OrderDTO>();
		for (OrderTable table : list) {
			OrderDTO dto = new OrderDTO();
			dto.setOid(table.getOrderId() + "");
			dto.setOrderDate(table.getDateOfOrder());
			dto.setStatus(table.getOrderStatus());
			dto.setDeliveryDate(table.getDateOfDelivery());
			dto.setAmount(table.getPrice());
			orderList.add(dto);
		}
		return orderList;
	}

	/**
	 * @return the orderList
	 */
	public List<OrderDTO> getOrderList() {
		return orderList;
	}

	/**
	 * @param orderList
	 *            the orderList to set
	 */
	public void setOrderList(List<OrderDTO> orderList) {
		this.orderList = orderList;
	}
}
